package com.karrysong.blog_springboot.controller;

import com.karrysong.blog_springboot.service.ArticleService;
import com.karrysong.blog_springboot.vo.ArticleVo;
import com.karrysong.blog_springboot.vo.Result;
import com.karrysong.blog_springboot.vo.params.ArticlePage;
import com.karrysong.blog_springboot.vo.params.ArticleParam;
import com.karrysong.blog_springboot.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName: ArticleController
 * @Author: KarrySong
 * @Date: 2022/8/2 15:14
 * @Description:
 */
@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 默认限制数量
     */
    private static Integer LIMIT_COUNTS = 5;
    /**
     * 默认分页页面索引
     */
    private static Integer PAGE_INDEX = 1;
    /**
     * 默认分页单页数量
     */
    private static Integer PAGE_SIZE = 10;

    @GetMapping("/hot")
    public Result hotArticle(){
        return articleService.hotArticle(LIMIT_COUNTS);
    }

    /**
     * 首页 最新文章
     * @return
     */
    @GetMapping("/new")
    public Result newArticles(){
        return articleService.newArticles(LIMIT_COUNTS);
    }

    /**
     * 首页 文章归档
     * @return
     */
    @GetMapping("/listArchives")
    public Result listArchives() {

        return articleService.listArchives();
    }


    /**
     * 查看博客详细内容
     * @param id 博客ID
     * @return
     */
    @GetMapping("/view/{id}")
    public Result findArticleById(@PathVariable("id") Long id, @RequestParam(required = false) Integer view) {
        if (view != null && view == 1){
            //增加阅读量（因为修改完成或者评论完都会调用此接口，故需要标识判断是否增加阅读量）
            articleService.addViewCounts(id);
            //
            System.out.println("增加浏览数");
        }
        return articleService.findArticleById(id);
    }

    @PostMapping
    public Result listArticle(@RequestBody(required = false) ArticlePage articlePage, HttpServletRequest request, HttpServletResponse response){
        if (articlePage == null){
            int pageSize = PAGE_SIZE;
            articlePage = new ArticlePage(PAGE_INDEX, pageSize);
        }

        return articleService.listArticle(articlePage);
    }

    @PostMapping("/admin")
    public Result adminListArticle(@RequestBody(required = false) ArticlePage articlePage){
        if (articlePage == null){
            int pageSize = PAGE_SIZE;
            articlePage = new ArticlePage(PAGE_INDEX, pageSize);
        }

        return articleService.adminListArticle(articlePage);
    }

    @PostMapping("/admin/{id}/{newStatus}")
    public Result changeStatus(@PathVariable("id") Long id, @PathVariable("newStatus") Integer newStatus){
        return articleService.changeArticleStatus(id, newStatus);
    }

    @PostMapping("/admin/delete/{id}")
    public Result deleteArticle(@PathVariable("id") Long id){
        return articleService.deleteArticle(id);
    }

    /**
     * 发布文章
     * @param articleParam 文章数据对象
     * @return
     */
    @PostMapping("/publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);
    }

    @PostMapping("/{id}")
    public Result articleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }

    @PostMapping("/search/{searchPar}")
    public Result searchArticleByName(@PathVariable String searchPar){
        return articleService.searchArticleByName(searchPar);
    }


}
